package com.apan.redispractice.controller;


import com.apan.redispractice.entity.User;
import com.apan.redispractice.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-24
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关操作接口")
public class UserController {

@Resource
IUserService userService;

    @GetMapping("/user/{id}")
    @Operation(summary = "获取用户信息", description = "根据ID查询用户详情")
    @Parameter(name = "id", description = "用户唯一标识", required = true, example = "59")
    User selectUserById  (@PathVariable long id){
        return userService.selectUserById(id);
    }

    // 新增：根据ID更新用户信息的接口
    @PutMapping("/updateUserById/{id}")
    @Operation(summary = "更新用户信息", description = "根据ID更新用户详情")
    @Parameter(name = "id", description = "用户唯一标识", required = true, example = "1")
    public ResponseEntity<Void> updateUserById(@PathVariable Long id, @RequestBody User updatedUser) {
        updatedUser.setId(id); // 确保ID一致
        boolean isUpdated = userService.updateById(updatedUser);
        if (isUpdated) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
